package com.yuhong.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuhong.springbootinit.model.dto.interview.InterviewStatusCountDTO;
import com.yuhong.springbootinit.model.entity.Interview;
import com.yuhong.springbootinit.model.enums.InterviewStatusEnum;
import com.yuhong.springbootinit.service.InterviewService;
import com.yuhong.springbootinit.mapper.InterviewMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 10467
* @description 针对表【interview】的数据库操作Service实现
* @createDate 2025-02-22 13:52:10
*/
@Service
public class InterviewServiceImpl extends ServiceImpl<InterviewMapper, Interview>
    implements InterviewService{

    public List<InterviewStatusCountDTO> countCreatedToTestedStatuses(String positionId) {
        List<Integer> targetStatuses = Arrays.asList(
                InterviewStatusEnum.CREATED.getCode(),
                InterviewStatusEnum.TESTING.getCode(),
                InterviewStatusEnum.TESTED.getCode()
        );

        return this.lambdaQuery()
                .eq(Interview::getPositionId, positionId)
                .in(Interview::getStatus, targetStatuses)
                .list()
                .stream()
                .collect(Collectors.groupingBy(Interview::getStatus, Collectors.counting()))
                .entrySet()
                .stream()
                .map(entry -> new InterviewStatusCountDTO(entry.getKey(), entry.getValue()))
                .sorted(Comparator.comparing(InterviewStatusCountDTO::getStatus))
                .collect(Collectors.toList());
    }


    public List<InterviewStatusCountDTO> countStatusFrom1To5(String positionId) {
        List<Integer> statusRange = Arrays.asList(1, 2, 3, 4, 5);

        return this.lambdaQuery()
                .eq(Interview::getPositionId, positionId)
                .in(Interview::getStatus, statusRange)
                .list()
                .stream()
                .collect(Collectors.groupingBy(Interview::getStatus, Collectors.counting()))
                .entrySet()
                .stream()
                .map(entry -> new InterviewStatusCountDTO(entry.getKey(), entry.getValue()))
                .sorted(Comparator.comparing(InterviewStatusCountDTO::getStatus))
                .collect(Collectors.toList());
    }



}




